//
//  MqProductViewController.m
//  Meiqi
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "MqProductViewController.h"
#import "MqMainBrandTableViewController.h"
#import "MqSingleton.h"
@interface MqProductViewController () <UIScrollViewDelegate>

@end

@implementation MqProductViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //self.view.backgroundColor = [UIColor yellowColor];
    
    [self addBar];
    
    [self addScrollView];
    
    //关闭自动布局
    self.automaticallyAdjustsScrollViewInsets = NO;
    
   // NSLog(@"%f --- %f",[MqSingleton shareData].lon,[MqSingleton shareData].lat);
    
}

//添加bar
- (void)addBar
{
    NSArray *TagArray = @[@"品牌"];
    
    
    for (int i = 0; i < 1; i ++) {
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        button.frame = CGRectMake(10 + i * (kScreenWidth / 6), 20, (kScreenWidth / 8) - 10, 40);
        //button.backgroundColor = [UIColor greenColor];
        [button setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
        
        
        button.tag = 100 + i;
        
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [button setTitle:TagArray[i] forState:(UIControlStateNormal)];
        [self.headBar addSubview:button];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake
                          (11 + i * (kScreenWidth / 6), 62, (kScreenWidth / 6) - 30, 2)];
        label.backgroundColor = [UIColor redColor];
        label.hidden = YES;
        label.tag = 1000 + i;
        [self.headBar addSubview:label];
        
    }
    
    self.searchButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _searchButton.frame = CGRectMake(kScreenWidth - 50, 20, 40, 40);
    [_searchButton setImage:[UIImage imageNamed:@"search"] forState:(UIControlStateNormal)];
//    [self.headBar addSubview:_searchButton];
    
    //进入找车模块  首先进入品牌界面
    UIButton *brandButton = [self.headBar viewWithTag:100];
    UILabel *label = [self.headBar viewWithTag:1000];
    [brandButton setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
    label.hidden = NO;
    
    
}

//bar上的按钮的点击方法
- (void)buttonClick:(UIButton *)button
{
    NSInteger index = button.tag - 100;
    [self changeColor:index];
    [UIView animateWithDuration:0.2 animations:^{
        [_scrollView setContentOffset:CGPointMake(kScreenWidth * index, 0) animated:YES];
    }];
}


//改变按钮的颜色的方法
- (void)changeColor:(NSInteger )index
{
    for (int i = 0; i < 4; i ++) {
        UIButton *button = [self.headBar viewWithTag:100 + i];
        UILabel *label = [self.headBar viewWithTag:1000 + i];
        
        if (i != index) {
            [button setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
            label.hidden = YES;
        }
        else
        {
            [button setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
            label.hidden = NO;
        }
    }
}


//添加ScrollView  在ScrollView上添加四个界面
- (void)addScrollView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64 - 49)];
    _scrollView.contentSize = CGSizeMake(kScreenWidth * 4, _scrollView.height);
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = YES;
    _scrollView.alwaysBounceHorizontal = YES;
    _scrollView.alwaysBounceVertical = NO;
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    
    //在轮播图上加上品牌界面
    MqMainBrandTableViewController *brandVC = [[MqMainBrandTableViewController alloc] init];
    brandVC.tableView.frame = CGRectMake(0, 0, kScreenWidth, _scrollView.height);
    [self addChildViewController:brandVC];
    [_scrollView addSubview:brandVC.tableView];
    
      
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat x = scrollView.contentOffset.x;
    CGFloat width = scrollView.frame.size.width;
    NSInteger num = x / width;
    [UIView animateWithDuration:0.2 animations:^{
        [self changeColor:num];
    }];

}


//一直滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
   
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
